package com.liiy.admin.controller;

import com.liiy.admin.bean.User;
import com.liiy.admin.exception.UserTooManyException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;

/**
 * 登录页
 */
@Slf4j
@Controller
public class IndexController {
    @GetMapping("login")
    public String loginPage(){
        return "login";
    }

    @PostMapping("/login")
    public String main(User user, HttpSession session, Model model){
        log.info(user.getUsername());
        log.info(user.getPassword());
        if(StringUtils.hasLength(user.getUsername()) && StringUtils.hasLength(user.getPassword())){
            session.setAttribute("userInfo",user);

            return "redirect:/main";
        }else{
            model.addAttribute("msg","账号不存在或者密码错误");

            return "login";
        }
    }

    @GetMapping("main")
    public String mainPage(){
        return "main";
    }

    @GetMapping("test")
    public String test(@RequestParam("name") String a){

        return "main";
    }

    @GetMapping("tomany")
    public String toMany(){
        throw new UserTooManyException();

//        return "main";
    }
}
